﻿using GYCTOAManagementSystemNewApi.Model.Tool;
using log4net;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace GYCTOAManagementSystemNewApi.Public
{
    /// <summary>
    /// 捕获全局异常信息
    /// </summary>
    public class CustomerExceptionFilterAttribute: IExceptionFilter
    {
        Message mes = new Message();
        private ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(CustomerExceptionFilterAttribute));
        /// <summary>
        /// 获取异常信息
        /// </summary>
        /// <param name="context"></param>
        public void OnException(ExceptionContext context)
        {
            Exception ex = context.Exception;
            string errorMsg = string.Format("【抛出信息】：{0} <br>【异常类型】：{1} <br>【异常信息】：{2} <br>【堆栈调用】：{3}", new object[] {ex.Data,
                ex.GetType().Name, ex.Message, ex.StackTrace });
            log.Error(errorMsg);
            #region 设置相关数据信息
            mes.Code = 500;
            mes.Msg = "";
            #endregion
            //context.HttpContext.Response.Body
        }
    }
}
